<template>
  <div v-loading="loading" element-loading-text="加载中">

    <!-- 表单区 -->
    <el-form :model="configForm" ref="configForm" class="mar-l-20">
      <el-form-item>
        <div><el-checkbox v-model="configForm.autoSyncFlag" true-label="1" false-label="0">开启商品库存自动同步</el-checkbox></div>
        <div style="line-height: 2.3; background-color: #f2f2f2" class="text-align-l pad-l-20 pad-t-5">
          <div>每隔<el-input v-model="configForm.syncFreq" class="width-100"></el-input>分钟自动同步一次商品库存</div>
          <div class="mar-l-30">（不要小于10分钟，推荐30分钟）</div>
        </div>
      </el-form-item>
    </el-form>
    <!-- 底部按钮区 -->
    <div slot="footer" class="dialog-footer">
      <el-button @click="doClose">取消</el-button>
      <el-button type="primary" @click="doSave">保存</el-button>
    </div>
  </div>
</template>

<script>
  import { getByCondition, save } from '@/api/warehouse/synInventoryConfig'

  export default {
    props: {
      showFlag: {
        type: Boolean,
        default() {
          return false
        }
      }
    },
    watch: {
      showFlag(val) {
        if (val) {
          this.init()
          this.loadConfig()
        }
      }
    },
    data() {
      return {
        // 同步设置表单
        configForm: {
          enterpriseId: '',
          warehouseId: '',
          autoSyncFlag: '0',
          syncFreq: '15'
        },
        loading: false
      }
    },
    created() {
      this.init()
      this.loadConfig()
    },
    methods: {
      // 初始化
      init() {
        const originData = this.$options.data()
        this.configForm = originData.configForm
        this.configForm.enterpriseId = this.getEnterpriseId()
        this.configForm.warehouseId = this.getWarehouseId()
      },
      // 加载配置事件
      loadConfig() {
        this.loading = true
        const form = {
          enterpriseId: this.configForm.enterpriseId,
          warehouseId: this.configForm.warehouseId
        }
        getByCondition(form).then(response => {
          this.loading = false
          if (response.isError) return
          const data = response.data
          if (data) {
            Object.assign(this.configForm, data)
            if (data.autoSyncFlag !== undefined) {
              this.configForm.autoSyncFlag = data.autoSyncFlag.toString()
            }
          } else {
            this.init()
          }
        })
      },
      // 同步设置保存事件
      doSave() {
        if (this.configForm.autoSyncFlag === '1') {
          if (!this.configForm.syncFreq || this.configForm.syncFreq * 1 < 10) {
            this.$message.error('同步订单间隔时间不能小于10分钟')
            return
          }
          if (this.configForm.syncFreq * 1 > 60) {
            this.$message.error('同步订单间隔时间不能大于60分钟')
            return
          }
        }
        this.loading = true
        save(this.configForm).then(response => {
          if (response.isError) {
            this.loading = false
            return
          }
          this.$message({ message: '保存成功', type: 'success' })
          this.loadConfig()
        })
      },
      // 取消事件
      doClose() {
        this.$emit('callback')
      }
    }
  }
</script>

<style lang="less" scoped>

</style>
